﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _15puzzle
{
    class Game
    {
        private int[,] Board = new int[4, 4];
        private int[,] FinishedBoard = new int[4, 4];

        public Game()
        {   
            int Number = 1;
            for(int i = 0; i < 4; i++)
            {
                for(int j = 0; j < 4; j++)
                {
                    Board[i,j] = Number;
                    FinishedBoard[i, j] = Number;
                    Number++;

                }
            }

            Board[3, 3] = 0;
            FinishedBoard[3, 3] = 0;
        }

        public void Randomize() // Randomizar genom swap
        {
            Random random = new Random();
            int[,] TempBoard = new int[1,1];

            for(int i = 0; i < 20000; i++)
            {
                int RandomRow = random.Next(0, 4);
                int RandomColumn = random.Next(0, 4);

                TempBoard[0, 0] = Board[RandomRow, RandomColumn];
                Board[RandomRow, RandomColumn] = Board[0, 0];
                Board[0, 0] = TempBoard[0, 0];
            }

           int zerox;
           int zeroy;
           for(int i = 0; i < 4; i++)       //Sätter sista brickan till 0
           {
               for (int j = 0; j < 4; j++)
               {
                   if(Board[i,j] == 0)
                   {
                       zerox = i;
                       zeroy = j;
                       TempBoard[0, 0] = Board[3, 3];
                       Board[3, 3] = Board[zerox, zeroy];
                       Board[zerox, zeroy] = TempBoard[0, 0];
                   }
               }
           }
        }

        public void PaintBoard()
        {
            Console.Clear();
            Console.Write("+----+----+----+----+\n");
            for (int i = 0; i < 4; i++)
            {
              for (int j = 0; j < 4; j++)
                {
                   if (Board[i, j] > 9)
                    {
                        if (j == 0)
                        {
                            Console.Write("| " + Board[i, j]);
                        }
                        else if (j == 3)
                        {
                            Console.Write(" | " + Board[i,j] + " |\n+----+----+----+----+\n");
                        }
                        else
                        {
                            Console.Write(" | " + Board[i,j]);
                        }
                    }
                    else
                    {
                        if (j == 0)
                        {
                            Console.Write("|  " + Board[i,j]);
                        }
                        else if (j == 3)
                        {
                            Console.Write(" |  " + Board[i,j] + " |\n+----+----+----+----+\n");
                        }
                        else
                        {
                            Console.Write(" |  " + Board[i,j]);

                        }
                    }
                }
            }
        }

        public void Move(string key)
        {
            switch(key[0])
            {
                case 'A' :
                    {
                        for(int i = 0; i < 4; i++ )
                        {
                            for(int j = 0; j < 4; j++ )
                            {
                                if(Board[i,j] == 0 && j > 0)
                                {
                                    Board[i, j] = Board[i,(j - 1)];
                                    Board[i,(j - 1)] = 0;
                                    goto default;

                                }
                            }
                        }
                        break;
                    }

                case 'W' :
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            for (int j = 0; j < 4; j++)
                            {
                                if (Board[i, j] == 0 && i > 0)
                                {
                                    Board[i, j] = Board[(i - 1), j];
                                    Board[(i - 1), j] = 0;
                                    goto default;
                                }
                            }
                        }
                        break;
                    }

                case 'S':
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            for (int j = 0; j < 4; j++)
                            {
                                if (Board[i, j] == 0 && i < 3)
                                {
                                    Board[i, j] = Board[(i + 1), j];
                                    Board[(i + 1), j] = 0;
                                    goto default;
                                }
                            }
                        }
                        break;
                    }
                 
                case 'D':
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            for (int j = 0; j < 4; j++)
                            {
                                if (Board[i, j] == 0 && j < 3)
                                {
                                    Board[i, j] = Board[i,(j + 1)];
                                    Board[i,(j + 1)] = 0;
                                    goto default;
                                }
                            }
                        }
                        break;
                    }  
  
                default:
                    break;
            }

        }

        public bool CheckBoard()
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (Board[i, j] != FinishedBoard[i, j])
                    {
                        return false;
                    }     
                }
            }
            return true;
        }

    }
}
